package com.yian.houseproperty.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yian.houseproperty.entity.User;
import com.yian.houseproperty.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yian
 * @since 2022-03-14
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;
    @GetMapping("/login/{userName}/{pwd}/{role}")
    public User login(@PathVariable("userName") String userName, @PathVariable("pwd") String pwd, @PathVariable("role")Integer role, HttpSession session){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("uname",userName).eq("password",pwd);
        User user=userService.getOne(wrapper);
        session.setAttribute("user",user);
        return user;
    }
    @GetMapping("/getOwners")
    public List<User> getOwners(){
        return userService.getOwners();
    }
    @GetMapping("/selectOne/{uid}")
    public User selectOne(@PathVariable Integer uid){
        return userService.getById(uid);
    }
    @GetMapping("/getAll")
    public List<User> getAll(){
        return  userService.list();
    }
    @PostMapping("/add")
    public void add(@RequestBody User user){
        userService.save(user);
    }
    @DeleteMapping("/del/{id}")
    public void del(@PathVariable Integer id){
        userService.removeById(id);
    }


}

